﻿using DevExpress.ExpressApp.DC;
using EasyXaf.EntityDesigner.BusinessObjects;
using EasyXaf.EntityDesigner.BusinessObjects.Entities.EntityProperties;
using System.Xml.Linq;

namespace EasyXaf.EntityDesigner.Editors.DetailViewEditors.Models;

/// <summary>
/// 属性编辑器
/// </summary>
[DomainComponent]
[XafDisplayName("属性编辑器")]
public class PropertyEditor : Component
{
    private string _caption;
    private CaptionLocation _captionLocation;
    private DefaultBoolean _showCaption;
    private string _entityPropertyPath;
    private EntityProperty _entityProperty;

    /// <summary>
    /// 标题
    /// </summary>
    [XafDisplayName("标题")]
    public string Caption
    {
        get => _caption;
        set => SetPropertyValue(ref _caption, value);
    }

    /// <summary>
    /// 标题位置
    /// </summary>
    [XafDisplayName("标题位置")]
    public CaptionLocation CaptionLocation
    {
        get => _captionLocation;
        set => SetPropertyValue(ref _captionLocation, value);
    }

    /// <summary>
    /// 显示标题
    /// </summary>
    [XafDisplayName("显示标题")]
    public DefaultBoolean ShowCaption
    {
        get => _showCaption;
        set => SetPropertyValue(ref _showCaption, value);
    }

    /// <summary>
    /// 组件名称
    /// </summary>
    [XafDisplayName("组件名称")]
    public string ComponentName
    {
        get => EntityProperty?.PropertyType switch
        {
            PropertyType.String => "文本",
            PropertyType.LongString => "长文本",
            PropertyType.Integer => "整数",
            PropertyType.Long => "长整数",
            PropertyType.Float => "浮点数",
            PropertyType.Double => "双浮点数",
            PropertyType.Decimal => "小数",
            PropertyType.DateTime => "日期时间",
            PropertyType.Boolean => "布尔型",
            PropertyType.Enumeration => "枚举",
            PropertyType.Guid => "Guid",
            PropertyType.Image => "图像",
            PropertyType.File => "文件",
            PropertyType.Entity => "实体",
            PropertyType.EntityCollection => "实体集合",
            _ => "未知"
        };
    }

    /// <summary>
    /// 实体属性路径
    /// </summary>
    [XafDisplayName("实体属性路径")]
    public string EntityPropertyPath
    {
        get => _entityPropertyPath;
        set => SetPropertyValue(ref _entityPropertyPath, value);
    }

    /// <summary>
    /// 实体属性
    /// </summary>
    [XafDisplayName("实体属性")]
    public EntityProperty EntityProperty
    {
        get => _entityProperty;
        set => SetPropertyValue(ref _entityProperty, value);
    }

    public PropertyEditor(Component parent, LayoutDesigner layoutDesigner)
        : base(parent, layoutDesigner)
    {
    }

    public override void WriteToXml(XElement element)
    {
        base.WriteToXml(element);

        if (!string.IsNullOrWhiteSpace(Caption))
        {
            SetXmlAttributeValue(element, "caption", Caption);
        }

        if (ShowCaption != DefaultBoolean.Default)
        {
            SetXmlAttributeValue(element, "show-caption", ShowCaption == DefaultBoolean.True ? "true" : "false");
        }

        if (CaptionLocation != CaptionLocation.Default)
        {
            SetXmlAttributeValue(element, "caption-location", CaptionLocation switch
            {
                CaptionLocation.Left => "left",
                CaptionLocation.Top => "top",
                CaptionLocation.Right => "right",
                CaptionLocation.Bottom => "bottom",
                _ => "left"
            });
        }

        element.Add(EntityPropertyPath);
    }

    public override void ReadFromXml(XElement element)
    {
        base.ReadFromXml(element);

        Caption = GetXmlAttributeValue<string>(element, "caption");

        var showCaption = GetXmlAttributeValue<string>(element, "show-caption");
        ShowCaption = showCaption switch
        {
            "true" => DefaultBoolean.True,
            "false" => DefaultBoolean.False,
            _ => DefaultBoolean.Default
        };

        var captionLocation = GetXmlAttributeValue<string>(element, "caption-location");
        CaptionLocation = captionLocation switch
        {
            "left" => CaptionLocation.Left,
            "top" => CaptionLocation.Top,
            "right" => CaptionLocation.Right,
            "bottom" => CaptionLocation.Bottom,
            _ => CaptionLocation.Default
        };

        EntityPropertyPath = element.Value;
        EntityProperty = LayoutDesigner.Entity.FindPropertyByPath(EntityPropertyPath);
    }

    public override XElement CreateXElement()
    {
        return new XElement("p");
    }
}
